package com.thuan.android3d;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLUtils;

public class Mouse {
	
	private float x;
	private float y;
	private FloatBuffer verticesBuffer;
	private ShortBuffer indicesBuffer;
	private int numOfIndices;
	
	public Mouse() {
		initBuffer();
	}
	
	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	
	private void initBuffer() {

		float[] vertices = {
				x,y,0,
				x,y+10,0,
				x+10,y+10,0,
				x+10,y,0,
		};
		
		short[] indices = {
				0,3,1,
				3,2,1
		};
		
		setVerticesBuffer(vertices);
		setIndicesBuffer(indices);
	}
	
	
	public void draw(GL10 gl) {
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
		gl.glColor4f(1, 0, 0, 1);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices, 
				GL10.GL_UNSIGNED_SHORT, indicesBuffer);
		
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
	}
	
	private void setVerticesBuffer(float[] vertices) {
		
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
		vbb.order(ByteOrder.nativeOrder());
		verticesBuffer = vbb.asFloatBuffer();
		verticesBuffer.put(vertices);
		verticesBuffer.position(0);
	}
	
	private void setIndicesBuffer(short[] indices) {
		
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2);
		ibb.order(ByteOrder.nativeOrder());
		indicesBuffer = ibb.asShortBuffer();
		indicesBuffer.put(indices);
		indicesBuffer.position(0);
		
		numOfIndices = indices.length;
	}
	
}
